package com.gwsdk.paysdk.acitvityV2.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gwsdk.paysdk.acitvityV2.GWMainMenuActivity;
import com.gwsdk.paysdk.acitvityV2.adapter.GWNoticeAdapter;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.entity.MsgTZModel;
import com.gwsdk.paysdk.http.process.MsgTZProcess;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class GWGameNoticeFragment extends GWBaseFragment implements View.OnClickListener{


    private TextView huodong;
    private TextView gongnue;
    private TextView gonggao;

    private View noDataView;
    private ProgressBar loadingView;
    private ListView listView;

    private GWNoticeAdapter adapter;
    List<MsgTZModel.ListBean> listData = new ArrayList<>();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setContentView() {
        return GWInflaterUtils.getLayout(getActivity(),"gw_fragment_game_notice");
    }

    @Override
    public void initView(View view) {
        huodong = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gw_huodong_btn"));
        gongnue = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gw_gongnue_btn"));
        gonggao = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gw_gonggao_btn"));
        huodong.setOnClickListener(this);
        gongnue.setOnClickListener(this);
        gonggao.setOnClickListener(this);

        loadingView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"progress_bar"));
        noDataView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"not_data_view"));
        listView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"list_view"));

        adapter = new GWNoticeAdapter(getActivity());
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                MsgTZModel.ListBean bean = listData.get(position);

                Bundle bundle = new Bundle();
                bundle.putString("title",bean.getTitle());
                bundle.putString("timer",bean.getCreate_time()+"");
                bundle.putString("viewUrl",bean.getUrl());
                GWNoticeDetailsFragment detailsFragment = new GWNoticeDetailsFragment();
                detailsFragment.setArguments(bundle);
                GWMainMenuActivity mainMenuActivity = (GWMainMenuActivity) getActivity();
                mainMenuActivity.addOtherFragment(detailsFragment);
            }
        });
    }

    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            loadingView.setVisibility(View.GONE);
            super.handleMessage(msg);
            switch (msg.what){
                case Constant.MSGTZ_SUCCESS:
                    MsgTZModel obj = (MsgTZModel) msg.obj;
                    List<MsgTZModel.ListBean> list = obj.getList();
                    listData = list;
                    if(list.size() == 0) {
                        listView.setVisibility(View.GONE);
                        noDataView.setVisibility(View.VISIBLE);
                    }else {
                        adapter.setListBeans(list);
                        listView.setVisibility(View.VISIBLE);
                        noDataView.setVisibility(View.GONE);
                    }
                    break;
                case Constant.MSGTZ_FAIL:
                    listView.setVisibility(View.GONE);
                    noDataView.setVisibility(View.VISIBLE);
                    ToastUtil.show(getActivity(),(String)msg.obj);
                    break;
            }
        }
    };


    private int type = 0;
    @Override
    public void getData() {
        loadListData(3);
    }

    private void loadListData(int type){
        if (this.type == type) return;

        loadingView.setVisibility(View.VISIBLE);
        listView.setVisibility(View.GONE);
        noDataView.setVisibility(View.GONE);
        this.type = type;
        MsgTZProcess msgTZProcess = new MsgTZProcess();
        msgTZProcess.setLimit("1");
        msgTZProcess.setType(type);
        msgTZProcess.post(handler);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == huodong.getId()) {
            huodong.getPaint().setFakeBoldText(true);
            gongnue.getPaint().setFakeBoldText(false);
            gonggao.getPaint().setFakeBoldText(false);
            huodong.setTextColor(Color.parseColor("#14B6FC"));
            gongnue.setTextColor(Color.parseColor("#E4E4E4"));
            gonggao.setTextColor(Color.parseColor("#E4E4E4"));
            loadListData(3);
        }else if (view.getId() == gongnue.getId()) {
            huodong.getPaint().setFakeBoldText(false);
            gongnue.getPaint().setFakeBoldText(true);
            gonggao.getPaint().setFakeBoldText(false);
            huodong.setTextColor(Color.parseColor("#E4E4E4"));
            gongnue.setTextColor(Color.parseColor("#14B6FC"));
            gonggao.setTextColor(Color.parseColor("#E4E4E4"));
            loadListData(5);
        }else if (view.getId() == gonggao.getId()) {
            huodong.getPaint().setFakeBoldText(false);
            gongnue.getPaint().setFakeBoldText(false);
            gonggao.getPaint().setFakeBoldText(true);
            huodong.setTextColor(Color.parseColor("#E4E4E4"));
            gongnue.setTextColor(Color.parseColor("#E4E4E4"));
            gonggao.setTextColor(Color.parseColor("#14B6FC"));
            loadListData(4);
        }
    }






}
